package com.gitee.chili.leisure.message;

import com.gitee.chili.leisure.ResponseResult;
import com.gitee.chili.leisure.RoomManager;
import com.gitee.chili.leisure.SimpleBeanFactory;
import com.gitee.chili.leisure.core.AbstractDefaultMessage;
import com.gitee.chili.leisure.core.Packet;
import com.gitee.chili.leisure.domain.Room;
import com.gitee.chili.leisure.enums.RoomType;
import com.gitee.chili.leisure.enums.Settlement;
import com.gitee.chili.leisure.game.RoomDetail;
import com.gitee.chili.leisure.model.HallModel;
import com.gitee.chili.leisure.model.MessageCmdEnum;
import com.gitee.chili.leisure.service.SpecificTaskTransferClient;
import com.gitee.chili.leisure.util.AuxiliaryRoom;
import com.gitee.chili.leisure.util.Connection;
import com.gitee.chili.leisure.util.MemberChannel;
import com.gitee.chili.leisure.util.Utils;
import com.google.protobuf.InvalidProtocolBufferException;
import io.netty.channel.Channel;
import io.netty.util.AttributeKey;
import lombok.extern.slf4j.Slf4j;

import java.net.InetSocketAddress;

/**
 * 创建好友房消息
 *
 * @author week solider
 * @since 2020.09.02
 */
@Slf4j
public class CreateFriendRoom extends AbstractDefaultMessage {

    /**
     * 房间信息
     */
    private RoomDetail roomDetail;

    public CreateFriendRoom(Packet packet, Channel channel) {
        super(packet, channel);
    }

    @Override
    protected void decoder(byte[] body) throws InvalidProtocolBufferException {
        HallModel.CreateFriendRoomCmd cfr = HallModel.CreateFriendRoomCmd.parseFrom(body);

        SpecificTaskTransferClient client = SimpleBeanFactory.getBean(SpecificTaskTransferClient.class);

        // 获取玩家id
        Long memberId = getMemberId();

        // 随机6位房间号
        Long roomNum = Utils.randomDigital();

        // 构建房间初始化信息
        Room instant = new Room()
                .setSettlementStatus(Settlement.WAIT)
                .setRoomType(RoomType.FRIEND_ROOM)
                .setGameType(cfr.getGameType())
                .setInnings(cfr.getInnings())
                .setGradeId(cfr.getGrade())
                .setUserId(memberId)
                .setRoomNum(roomNum);

        // 写入持久层
        ResponseResult<RoomDetail> result = client.createRoom(instant);
        asserts(result);
        roomDetail = result.getData();

        // 添加到缓存
        RoomManager.storage(roomNum, roomDetail);

        // 缓存消息信道
        MemberChannel memberChannel = MemberChannel.builder()
                .memberId(memberId)
                .channel(getChannel())
                .build();

        // 存储消息信道
        Connection.broadcaster().set(roomNum, memberChannel);
    }

    @Override
    public Packet parseBody() {

        // 缓存房间号
        getChannel().attr(AttributeKey.valueOf("roomNum")).set(roomDetail.getRoomNum());

        // 获取IP地址
        InetSocketAddress address = (InetSocketAddress) getChannel().remoteAddress();
        String ip = address.getAddress().getHostAddress();

        HallModel.NotifyRoomResult.Builder builder = AuxiliaryRoom.notifyRoom(roomDetail, ip);

        return Packet.newPacket(MessageCmdEnum.Command.NOTIFY_ROOM_INFO, builder.build().toByteArray());
    }

}
